package org.cy.example.domain.repository

import org.cy.evolution.framework.domain.repository.BaseRepository
import org.cy.evolution.framework.ui.listener.OnViewRequestListener
import org.cy.evolution.ioc.annotation.Component
import org.cy.evolution.net.http.converter.ConverterType
import org.cy.evolution.net.http.exception.ApiException
import org.cy.evolution.net.http.rx.AbstractObserver
import org.cy.evolution.net.http.scheduler.RxScheduler
import org.cy.example.domain.entity.CustomResponse
import org.cy.example.domain.entity.ExamEntity
import org.cy.example.domain.service.ExamService

@Component
class ExamRepository : BaseRepository<ExamService>() {

    override fun getResponseType(): ConverterType {
        return ConverterType.JSON
    }

    override fun getClazz(): Class<ExamService> {
        return ExamService::class.java
    }

    override fun getBaseUrl(): String {
        return ""
    }

    fun getExamData(listener: OnViewRequestListener<List<ExamEntity>>) {
        service.getData().compose(RxScheduler.io_main())
                .subscribe(object : AbstractObserver<CustomResponse<List<ExamEntity>>>() {
                    override fun onRequest() {
                        listener.onSendRequest()
                    }

                    override fun onSuccess(response: CustomResponse<List<ExamEntity>>?) {
                        if (null != response && response.success()) {
                            listener.onResult(response.data)
                        } else {
                            onFailure(ApiException(-1, "Response data is null"))
                        }
                    }

                    override fun onFailure(aex: ApiException?) {
                        listener.onError(aex)
                    }

                    override fun onFinished() {
                        listener.onFinished()
                    }

                })
    }
}